#pragma once
#include <ArduinoJson.h>
#include <LittleFS.h>

class AppConfig
{
private:
    int initFlag = 0;
    String configName = "";
    String jsonData = "{\"rs485_id\":\"1\", \"rs485_baud\":\"9600\"}";

    // 485站号
    int rs485_id;
    // 485波特率
    int rs485_baud;

    // 初始化 LittleFS
    bool initializeLittleFS()
    {
#ifdef ESP32
        if (!LittleFS.begin(true))
#else
        if (!LittleFS.begin())
#endif
        {
            Serial.println(F("[AppConfig] LittleFS 初始化失败"));
            return false;
        }

        return true;
    }

    // 配置写入
    void write()
    {
        if (!initializeLittleFS())
        {
            return;
        }

        File config = LittleFS.open(this->configName, "w");
        if (!config)
        {
            Serial.println("[AppConfig] 无法打开配置文件：" + this->configName);
            LittleFS.end();
            return;
        }
        
        String data = "{\"rs485_id\":\"" + (String)this->rs485_id + "\",\"rs485_baud\":\"" + (String)this->rs485_baud + "\"}";
        if (config.print(data) != data.length())
        {
            Serial.println("[AppConfig] 配置：" + this->configName + " 写入失败");
            config.close();
            LittleFS.end();
            return;
        }
        config.close();
        LittleFS.end();

        this->loadConfig();
    }

public:
    AppConfig(String configName)
    {
        this->configName = configName;
    }

    // 读取配置
    bool loadConfig()
    {
        if (this->initFlag == 0)
        {
            Serial.println();
            Serial.println();
            this->initFlag = 1;
        }

        if (!initializeLittleFS())
        {
            return false;
        }

        File config = LittleFS.open(this->configName, "r");
        if (!config)
        {
            Serial.print(F("[AppConfig] 无法打开配置文件："));
            Serial.println(this->configName);
        }
        else
        {
            String jsonData = config.readString();
            if (jsonData.length() > 0)
            {
                this->jsonData = jsonData;
            }else{
                Serial.println(F("[AppConfig] 数据为空"));
            }
            
            config.close();
        }
        LittleFS.end();

        Serial.print(F("[AppConfig] 配置："));
        Serial.print(this->configName);
        Serial.print(F(" ，数据："));
        Serial.println(this->jsonData);

        DynamicJsonDocument JSON_Buffer(this->jsonData.length() + 20);
        if (deserializeJson(JSON_Buffer, this->jsonData))
        {
            Serial.print(F("[AppConfig] 解析JSON错误"));
            return false;
        }

        JsonObject ConfigData = JSON_Buffer.as<JsonObject>();
        this->rs485_id = ConfigData["rs485_id"].as<int>();
        this->rs485_baud = ConfigData["rs485_baud"].as<int>();

        Serial.println(F("[AppConfig] 解析成功"));
        return true;
    }

    // 获取配置
    int rs485_id_Get()
    {
        return this->rs485_id;
    }
    int rs485_baud_Get()
    {
        return this->rs485_baud;
    }

    // 更新配置
    void rs485_id_Set(int value)
    {
        this->rs485_id = value;
        this->write();
    }
    void rs485_baud_Set(int value)
    {
        this->rs485_baud = value;
        this->write();
    }

    // 清除配置
    void clear()
    {
        if (!initializeLittleFS())
        {
            return;
        }

        if (LittleFS.remove(this->configName))
        {
            Serial.print(F("[AppConfig] 配置："));
            Serial.print(this->configName);
            Serial.println(F(" 已清除"));
        }
        else
        {
            Serial.print(F("[AppConfig] 无法清除配置："));
            Serial.println(this->configName);
        }

        LittleFS.end();
    }
};
